
import school30.School30App;
import school30.ui.forms.Button;
import school30.ui.forms.Form;
import school30.ui.forms.TextField;
import school30.ui.forms.WidgetPosition;

public class FormSample extends School30App {

    Form form;
    Button pressMeButton;
    Button counterButton;
    TextField editMeTextField;
    Runnable pressMeAction;
    Runnable counterPressAction;
    
    int counter;
    
    Object lock = new Object();

    public void startApp() {
        form = new Form(this);
        pressMeButton = new Button();
        pressMeButton.setPosition(new WidgetPosition(10, 20, getWidth() - 20, 30));
        form.addWidget(pressMeButton);
        counterButton = new Button();
        counterButton.setPosition(new WidgetPosition(10, 60, getWidth() / 2 - 5, 30));
        form.addWidget(counterButton);
        editMeTextField = new TextField();
        editMeTextField.setPosition(new WidgetPosition(10, 100, getWidth() - 20, 30));
        form.addWidget(editMeTextField);
        pressMeAction = new Runnable() {
            public void run() {
                counterButton.setText(Integer.toString(++counter));
            }
        };
        pressMeButton.setAction(pressMeAction);
        counterPressAction = new Runnable() {
            public void run() {
                counter = 0;
                counterButton.setText(Integer.toString(counter));
            }
        };
        counterButton.setAction(counterPressAction);        
        synchronized (lock) {
            try {
                lock.wait();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void terminateApp() {
        synchronized (lock) {
            lock.notifyAll();
        }
    }

    public void keyPressed(int keycode) {
        form.keyPressed(keycode);
    }

    public void keyReleased(int keycode) {
        form.keyReleased(keycode);
    }
}
